<?php

namespace App\Handlers;

use App\Facades\DeviceService;
use App\Models\Device;
use App\Models\User;
use App\Models\UserInfo;
use Cache;
use Exception;

class InnerHandler
{
	/**
	 * 用户给内部监听发指令时，此方法把指令发送到设备
	 * @param  [type] $serv    
	 * @param  [type] $fd      
	 * @param  [type] $from_id 
	 * @param  [type] $data    
	 * @return [type]          
	 */
	public function onReceive($serv, $fd, $from_id, $data)
	{
		try{
			$dataArray = explode(' ', $data);
			$cmd = $dataArray[0];
			$num = $dataArray[1];
			$deviceCode = $dataArray[2];
			$orderNumber = $dataArray[3];
			
			$cmd = DeviceService::getCmd($cmd, $num, $deviceCode, $orderNumber, $dataArray[4]);
			if(!$cmd){
				return ;
			}

			$deviceFd = Cache::get($deviceCode.'fd');
			if($serv->exist($deviceFd)){
				$serv->send($deviceFd, $cmd);
			}
		}catch(Exception $e){
			if($e->getLine() != 128){
				logger(['InnerHandler@onReceive' => [
					'line' => $e->getLine(),
					'message' => $e->getMessage(),
					]]);
			}
		}
	}
}